package com.useeinfo.oa.modules.inspection.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.DictInfo;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.inspection.enums.InsApplyTypeEnum;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.Date;

/**
 * 申请记录表
 */
@Entity
@Table(name="ins_apply_order")
public class InsApplyOrder extends SuperClassEntity<InsApplyOrder> {
    /**
     * 关联工单
     */
    @ManyToOne
    @JoinColumn(name = "inspection_order_id")
    private InspectionOrder inspectionOrder;

    /**
     * 申请人
     *
     */
    @ManyToOne
    @JoinColumn(name = "apply_user_id")
    private SystemUser applyUser;

    /**
     * 申请时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date applyDate;
    /**
     * 原因
     */
    private String reason;
    /**
     * 分类
     */
    @Enumerated(value = EnumType.STRING)
    private InsApplyTypeEnum insapplyTypeEnum;
    /**
     * 延期时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date delayDate;
    /**
     * 当前审核等级，审核通过和驳回之后等级为0
     */
    private int level=1;
    /**
     * 是否有效，审核通过的就算有效
     */
    private boolean effective=true;
    /**
     * 所属企业
     */
    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    /**
     * 申请分类
     */
    @ManyToOne
    @JoinColumn(name = "dictInfo_id")
    private DictInfo dictInfo;

    public SystemUser getApplyUser() {
        return applyUser;
    }

    public void setApplyUser(SystemUser applyUser) {
        this.applyUser = applyUser;
    }

    public Date getApplyDate() {
        return applyDate;
    }

    public void setApplyDate(Date applyDate) {
        this.applyDate = applyDate;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public InspectionOrder getInspectionOrder() {
        return inspectionOrder;
    }

    public void setInspectionOrder(InspectionOrder inspectionOrder) {
        this.inspectionOrder = inspectionOrder;
    }

    public Date getDelayDate() {
        return delayDate;
    }

    public void setDelayDate(Date delayDate) {
        this.delayDate = delayDate;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public Boolean getEffective() {
        return effective;
    }

    public void setEffective(Boolean effective) {
        this.effective = effective;
    }

    public InsApplyTypeEnum getInsapplyTypeEnum() {
        return insapplyTypeEnum;
    }

    public void setInsapplyTypeEnum(InsApplyTypeEnum insapplyTypeEnum) {
        this.insapplyTypeEnum = insapplyTypeEnum;
    }

    public boolean isEffective() {
        return effective;
    }

    public void setEffective(boolean effective) {
        this.effective = effective;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public DictInfo getDictInfo() {
        return dictInfo;
    }

    public void setDictInfo(DictInfo dictInfo) {
        this.dictInfo = dictInfo;
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("name", StringConverters.null2String(getInspectionOrder().getOrderName()));
        jsonObject.put("code", StringConverters.null2String(getInspectionOrder().getOrderCode()));
        jsonObject.put("applyUser", StringConverters.null2String(getApplyUser().getUserName()));
        jsonObject.put("applyDate", StringConverters.null2String(getApplyDate()));
        jsonObject.put("reason", StringConverters.null2String(getReason()));
        jsonObject.put("applyType", StringConverters.null2String(getInsapplyTypeEnum().getName()));
        jsonObject.put("level", StringConverters.null2String(getLevel()));
        jsonObject.put("enterpriseName",getEnterprise()!=null?getEnterprise().getEnterpriseName():"");
        jsonObject.put("dictInfoName",getDictInfo() == null?"":getDictInfo().getPropertyName());
        return jsonObject;
    }
}
